package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.service.DashboardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 仪表盘管理Controller
 * Created by macro on 2023/6/15.
 */
@Controller
@Api(tags = "DashboardController")
@Tag(name = "DashboardController", description = "仪表盘管理")
@RequestMapping("/api/dashboard")
public class DashboardController {
    @Autowired
    private DashboardService dashboardService;

    @ApiOperation("获取仪表盘综合统计数据")
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getStatistics() {
        return CommonResult.success(dashboardService.getStatistics());
    }

    @ApiOperation("获取订单图表数据")
    @RequestMapping(value = "/orderChart", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getOrderChart(@RequestParam(value = "startDate", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                      @RequestParam(value = "endDate", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return CommonResult.success(dashboardService.getOrderChart(startDate, endDate));
    }
    
    @ApiOperation("手动刷新统计数据缓存")
    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult refreshStatistics() {
        dashboardService.refreshStatistics();
        return CommonResult.success("统计数据缓存已刷新");
    }
} 